package com.vic.kjlink.order.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.vic.kjlink.order.mapper.StartMapper;
import com.vic.kjlink.order.model.Food;
import com.vic.kjlink.order.model.Start;
import com.vic.kjlink.order.model.StartDetail;

import pers.vic.boot.base.model.BaseSelectModel;
import pers.vic.boot.base.service.BaseService;

/**
 * @author Vic.xu
 * @description:开启点餐表 Service
 * @date: 2021-08-30 09:40
 */
@Service
public class StartService extends BaseService<StartMapper, Start> {

    @Resource
    private FoodService foodService;

    @Override
    protected boolean hasAttachment() {
        return false;
    }

    @Override
    public Start findById(int id) {
        Start entity = super.findById(id);
        if (entity != null) {
            entity.setDetails(mapper.details(id));
        }
        return entity;
    }

    @Override
    public int insert(Start entity) {
        int result = super.insert(entity);
        if (!CollectionUtils.isEmpty(entity.getDetails())) {
            mapper.inserDetails(entity.getDetails());
        }
        return result;
    }

    @Override
    public int update(Start entity) {
        super.update(entity);
        List<StartDetail> details = entity.getDetails();
        mapper.deleteDetais(entity.getId());
        if (!CollectionUtils.isEmpty(details)) {
            mapper.inserDetails(details);
        }
        return super.update(entity);
    }

    @Override
    public int delete(int id) {
        mapper.deleteDetais(id);
        return super.delete(id);
    }

    /**
     * 开启点餐的详情
     * 
     * @param sid
     * @return
     */
    public List<StartDetail> details(int sid) {
        return mapper.details(sid);
    }

    public List<BaseSelectModel> initStartSelectDatas(Integer id) {
        List<StartDetail> details = id == null ? new ArrayList<>() : this.details(id);
        Map<Integer, StartDetail> detailMap =
            details.stream().collect(Collectors.toMap(StartDetail::getFoodId, d -> d, (k1, k2) -> k1));

        Food lookup = new Food();
        lookup.setEnable(true);
        List<Food> foodList = foodService.list(lookup);
        List<BaseSelectModel> result = foodList.stream().map(food -> {
            BaseSelectModel model = new BaseSelectModel(food.getId(), food.getFullDescribe());
            // 如果还没有配置 则根据是否是默认套餐 选中
            if (id == null) {
                model.setSelected(food.getDefaults());
            } else {
                model.setSelected(detailMap.containsKey(food.getId()));
            }
            return model;
        }).collect(Collectors.toList());

        return result;
    }

    public Integer getLast() {
        return mapper.getLast();
    }

    public Start getLastOpen() {
        Start last = mapper.getLastOpen();
        if (last != null) {
            // 不合法的也不取
            if (!last.isLegalStatus()) {
                updateStatus(last.getId(), false);
                last = new Start();
            }
        } else {
            last = new Start();
        }

        return last;
    }

    public int updateStatus(int id, boolean enbale) {
        Start start = new Start();
        start.setId(id);
        start.setEnable(false);
        return update(start);
    }

    /**
     * 保存开启点餐信息
     * 
     * @param entity
     * @param codes
     */
    public void save(Start entity, int[] codes) {
        entity.setDate(new Date());
        save(entity);
        int id = entity.getId();
        // 保存详细信息
        // 删除详情
        mapper.deleteDetais(id);
        if (codes == null) {
            return;
        }
        // 重新保存
        List<StartDetail> details = new ArrayList<>();

        for (int code : codes) {
            Food food = foodService.findById(code);
            if (food == null) {
                continue;
            }
            StartDetail detail = new StartDetail();
            detail.setFoodId(code);
            detail.setFoodDescribe(food.getFullDescribe());
            detail.setSid(id);
            details.add(detail);
        }
        mapper.inserDetails(details);
    }

    /**
     * 同步状态：把过期的开启点餐置为关闭
     */
    public int synchronizationStartStatus() {
        return mapper.synchronizationStartStatus();
    }
}
